package junweixu.jreader;

import java.io.IOException;


import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;

public class MyReaderActivity extends Activity
{
    /** Called when the activity is first created. */
    private BookPage mBookPage;
    private TXTPageFactory mPageFactory;
    Bitmap mCurPageBitmap, mNextPageBitmap;
    Canvas mCurPageCanvas, mNextPageCanvas;
    private int mScreenWidth;
    private int mScreenHeight;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        getScrrenSize();
        mBookPage = new BookPage(this, mScreenWidth, mScreenHeight);
        setContentView(mBookPage);

        mCurPageBitmap = Bitmap.createBitmap(mScreenWidth, mScreenHeight,
                Bitmap.Config.ARGB_8888);
        mNextPageBitmap = Bitmap.createBitmap(mScreenWidth, mScreenHeight,
                Bitmap.Config.ARGB_8888);
        mCurPageCanvas = new Canvas(mCurPageBitmap);
        mNextPageCanvas = new Canvas(mNextPageBitmap);
        Intent intent = getIntent();
        String mPath = intent.getStringExtra("path");
        mPageFactory = new TXTPageFactory(mScreenWidth, mScreenHeight, mPath, 0);
        mPageFactory.setBgColor(0xFF000000);
        mPageFactory.setFontColor(0xFFFFFFFF);

        try
        {
            mPageFactory.openBook();
            mPageFactory.onDraw(mCurPageCanvas);
        } catch (Exception e)
        {
            Toast.makeText(this, "文件打开出错！", Toast.LENGTH_SHORT).show();
        }
        mBookPage.setBitmaps(mCurPageBitmap, mCurPageBitmap);

        mBookPage.setOnTouchListener(new OnTouchListener()
        {

            @Override
            public boolean onTouch(View v, MotionEvent event)
            {
                // TODO Auto-generated method stub
                if (v == mBookPage)
                {
                    if (event.getAction() == MotionEvent.ACTION_DOWN)
                    {
                        mBookPage.abortAnimation();
                        mPageFactory.onDraw(mCurPageCanvas);
                        if (event.getX() < mScreenWidth / 2)
                        {
                            try
                            {
                                mPageFactory.prePage();
                            } catch (IOException e)
                            {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                            if (mPageFactory.isfirstPage())
                            {
                                Toast.makeText(MyReaderActivity.this, "已经是第一页",
                                        Toast.LENGTH_SHORT).show();
                                return false;
                            }
                            mPageFactory.onDraw(mNextPageCanvas);
                        } else
                        {
                            try
                            {
                                mPageFactory.nextPage();
                            } catch (IOException e)
                            {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                            if (mPageFactory.islastPage())
                            {
                                Toast.makeText(MyReaderActivity.this,
                                        "已经是最后一页", Toast.LENGTH_SHORT).show();
                                return false;
                            }
                            mPageFactory.onDraw(mNextPageCanvas);
                        }
                        mBookPage.setBitmaps(mCurPageBitmap, mNextPageBitmap);
                    }
                    return mBookPage.doTouchEvent(event);
                }
                return false;
            }
        });
    }

    private void getScrrenSize()
    {
        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        mScreenWidth = dm.widthPixels;
        mScreenHeight = dm.heightPixels;
    }
}